const WXAPI = require('apifm-wxapi')
const regeneratorRuntime = require('../../utils/runtime')
//获取应用实例
var app = getApp()
Page({
  data: {
    pickerRegionRange: [],
    pickerSelect:[0, 0, 0],
    showRegionStr: '请选择',
    defChecked:false,
  },
  initRegionPicker () {
    WXAPI.province().then(res => {
      console.log("initRegionPicker", res)
      if (res.retCode === "200") {
        let _pickerRegionRange = []
        _pickerRegionRange.push(res.data)
        _pickerRegionRange.push([{ areaName: '请选择' }])
        _pickerRegionRange.push([{ areaName: '请选择' }])
        console.log("0000-=",_pickerRegionRange)
        this.data.pickerRegionRange = _pickerRegionRange
        this.bindcolumnchange({ detail: { column: 0, value: 0 } })
      }
    })
  },
  async initRegionDB (pname, cname, dname) {
    this.data.showRegionStr = pname + ' ' + cname + ' '  + dname
    let pObject = undefined
    let cObject = undefined
    let dObject = undefined
    if (pname) {
      const index = this.data.pickerRegionRange[0].findIndex(ele=>{
        console.log("name:",ele)
        return ele.areaName == pname
      })
      console.log('pindex', index)
      if (index >= 0) {
        this.data.pickerSelect[0] = index
        pObject = this.data.pickerRegionRange[0][index]
      }
    }    
    if (!pObject) {
      return
    }
    const _cRes = await WXAPI.nextRegion(pObject.areaCode)
    if (_cRes.retCode === "200") {
      this.data.pickerRegionRange[1] = _cRes.data
      if (cname) {
        const index = this.data.pickerRegionRange[1].findIndex(ele => {
          return ele.areaName == cname
        })
        if (index >= 0) {
          this.data.pickerSelect[1] = index
          cObject = this.data.pickerRegionRange[1][index]
        }
      }
    }    
    if (!cObject) {
      return
    }
    const _dRes = await WXAPI.nextRegion(cObject.areaCode)
    console.log("nextRegion=", _dRes)
    if (_dRes.retCode === "200") {
      this.data.pickerRegionRange[2] = _dRes.data
      if (dname) {
        const index = this.data.pickerRegionRange[2].findIndex(ele => {
          return ele.areaName == dname
        })
        if (index >= 0) {
          this.data.pickerSelect[2] = index
          dObject = this.data.pickerRegionRange[2][index]
        }
      }
    }
    this.setData({
      pickerRegionRange: this.data.pickerRegionRange,
      pickerSelect: this.data.pickerSelect,
      showRegionStr: this.data.showRegionStr,
      pObject: pObject,
      cObject: cObject,
      dObject: dObject
    })
  },
  bindchange: function(e) {
    console.log("bindChange:",e)
    const pObject = this.data.pickerRegionRange[0][e.detail.value[0]]
    const cObject = this.data.pickerRegionRange[1][e.detail.value[1]]
    const dObject = this.data.pickerRegionRange[2][e.detail.value[2]]
    const showRegionStr = pObject.areaName + cObject.areaName + dObject.areaName
    this.setData({
      pObject: pObject,
      cObject: cObject,
      dObject: dObject,
      showRegionStr: showRegionStr
    })
  },
  bindcolumnchange: function(e) {
    const column = e.detail.column;
    const index = e.detail.value;
    //console.log('column:', column)
    //console.log('index:', index)
    const regionObject = this.data.pickerRegionRange[column][index]
    //console.log('bindcolumnchange', regionObject)
    if (column === 2) {
      this.setData({
        pickerRegionRange: this.data.pickerRegionRange
      })
      return
    }
    if (column === 1) {
      this.data.pickerRegionRange[2] = [{ areaName: '请选择' }]
    }
    if (column === 0) {
      this.data.pickerRegionRange[1] = [{ areaName: '请选择' }]
      this.data.pickerRegionRange[2] = [{ areaName: '请选择' }]
    }
    // // 后面的数组全部清空
    // this.data.pickerRegionRange.splice(column+1)
    // 追加后面的一级数组
    WXAPI.nextRegion(regionObject.areaCode).then(res => {
      console.log("regionObject", res)
      if (res.retCode === "200") {
        this.data.pickerRegionRange[column + 1] = res.data     
      }
      this.bindcolumnchange({ detail: { column: column + 1, value: 0 } })
    })
  },
  // 设置默认地址
  defBtn(){
    if (this.data.defChecked){
      this.setData({
        defChecked:false,
      })
    }else{
      this.setData({
        defChecked: true,
      })
    }
  },

  bindSave: function(e) {
    console.log("bindSave",e)
   // WXAPI.addTempleMsgFormid(wx.getStorageSync('token'), 'form', e.detail.formId)
    var that = this;
    var linkMan = e.detail.value.linkMan;
    var address = e.detail.value.address;
    var mobile = e.detail.value.mobile;
    var code = e.detail.value.code;
    if (linkMan == ""){
      wx.showModal({
        title: '提示',
        content: '请填写联系人姓名',
        showCancel:false
      })
      return
    }
    if (mobile == ""){
      wx.showModal({
        title: '提示',
        content: '请填写手机号码',
        showCancel:false
      })
      return
    }
    if (!this.data.pObject || !this.data.cObject){
      wx.showModal({
        title: '提示',
        content: '请选择地区',
        showCancel:false
      })
      return
    }
    if (address == ""){
      wx.showModal({
        title: '提示',
        content: '请填写详细地址',
        showCancel:false
      })
      return
    }
    // if (code == ""){
    //   wx.showModal({
    //     title: '提示',
    //     content: '请填写邮编',
    //     showCancel:false
    //   })
    //   return
    // }
    let apiResult
    if (that.data.id) {
      console.log("update data===", this.data)
      apiResult = WXAPI.updateAddress({
        id: that.data.id,
        ifDefault: this.data.defChecked ? 'Y':'N',
        provinceCode: this.data.pObject.areaCode,
        recvProvince: this.data.pObject.areaName,
        cityCode: this.data.cObject.areaCode,
        recvCity: this.data.cObject.areaName,
        districtCode: this.data.dObject.areaCode,
        recvDistrict: this.data.dObject.areaName,
        recvAddress: address,
        // recvZip: code,
        recvName: linkMan,
        recvPhone: mobile,
        recvMobile: mobile
      })
    } else {
      console.log("create data===",this.data)
      apiResult = WXAPI.createAddr({
        ifDefault: this.data.defChecked ? 'Y' : 'N',
        provinceCode: this.data.pObject.areaCode,
        recvProvince: this.data.pObject.areaName,
        cityCode: this.data.cObject.areaCode,
        recvCity: this.data.cObject.areaName,
        districtCode: this.data.dObject.areaCode,
        recvDistrict: this.data.dObject.areaName,
        recvAddress: address,
        recvZip: code,
        recvName: linkMan,
        recvPhone: mobile,
        recvMobile: mobile
      })
    }
    apiResult.then(function (res) {
      if (res.retCode != "200") {
        // 登录错误 
        wx.hideLoading();
        wx.showModal({
          title: '失败',
          content: res.msg,
          showCancel: false
        })
        return;
      }
      // 跳转到结算页面
      wx.navigateBack({})
    })
  },
  onLoad: function (e) {
    const _this = this
    _this.initRegionPicker() // 初始化省市区选择器
    console.log("e999999===", e)
    if (e.id) { // 修改初始化数据库数据
      wx.setNavigationBarTitle({
        title: '修改地址'
      })
      WXAPI.addressDetail(e.id).then(function (res) {
        console.log("数据初始化完成后，获取地址详情",res)
        if (res.retCode === "200") {
          _this.setData({
            id: e.id,
            addressData: res.data,
            showRegionStr: res.data.recvProvince + res.data.recvCity + res.data.recvDistrict,
            defChecked: res.data.ifDefault == 'Y' ? true : false,
          });
          _this.initRegionDB(res.data.recvProvince, res.data.recvCity, res.data.recvDistrict)
          return;
        } else {
          wx.showModal({
            title: '提示',
            content: '无法获取收货地址数据',
            showCancel: false
          })
        }
      })
    }else{
      wx.setNavigationBarTitle({
        title: '添加地址'
      })
    }
    // 屏蔽分享
    wx.hideShareMenu();
  },
  deleteAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          WXAPI.deleteAddress(id).then(function () {
            wx.navigateBack({})
          })
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },
  readFromWx : function () {
    const _this = this
    wx.chooseAddress({
      success: function (res) {
        console.log("res6666666",res)
        _this.initRegionDB(res.provinceName, res.cityName, res.countyName)
        _this.setData({
          wxaddress: res
        });
      }
    })
  }
})
